package managerInterface;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.LayoutManager;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JScrollPane;
import javax.swing.JTabbedPane;
import javax.swing.JTextField;
import javax.swing.UIManager;
import javax.swing.border.Border;

public class ManagerInterface extends JFrame implements ActionListener, MouseListener{

	private JTabbedPane tab;
	private JPanel panelStudent, panelTeacher, panelClass, panelFaculty;
	private int width, height;
	private JLabel lblFaculty, lblStudent, lblClass, lblTeacher;
	private JLabel lblStudentFirstName, lblStudentLastName, lblStudentSex, lblStudentBirthDay, lblStudentHometown
	, lblStudentMajor, lblStudentClass, lblStudentFacculty, lblStudentInfo, lblStudentImage;
	private JTextField tfStudentFirstName, tfStudentLastName, tfStudentBirthDay, tfStudentHometown;
	private JPanel[] panelElement = new JPanel[10];
	private JPanel panelstudentInfo, panelStudentImage;
	private Box boxFaculty;
	private Box boxStudentFirstName, boxStudentLastName, boxStudentSex, boxStudentBirthDay, boxStudentHometown
	, boxStudentMajor, boxStudentClass, boxStudentFacculty, boxButton, boxMain, boxStudentInfo, boxStudentImage;
	private JComboBox cbbStudentFaculty, cbbStudentMajor, cbbStudentClass;
	private JFileChooser fcStudentImage;
	private JButton bntAccept, bntCancel;
	private JRadioButton rdMale, rdFemale;
	private ButtonGroup grSex;
	String choosedFalcuty = "Chọn", choosedClass = "Chọn", choosedMajor = "Chọn";
	public ManagerInterface() throws Exception{
		initialInterface();
	}

	public static void main(String[] args) throws Exception{
		UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
		new ManagerInterface().setVisible(true);
	}

	private void initialInterface() throws Exception{
		width = getScreenWorkingWidth();
		height = getScreenWorkingHeight();
		setSize(width, height);
		setDefaultCloseOperation(EXIT_ON_CLOSE);
		setResizable(false);
		setTitle("Management Interface");
		panelFaculty = new JPanel();
		panelTeacher = new JPanel();
		panelClass = new JPanel();
		panelStudent = new JPanel();
		boxFaculty = Box.createVerticalBox();
		
		tab = new JTabbedPane();
		initialFacultyTab();
		initialStudentTab();
		tab.addTab("panelFaculty", boxFaculty);
		tab.addTab("panelTeacher", panelTeacher);
		tab.addTab("panelClass", panelClass);
		panelstudentInfo.setBorder(BorderFactory.createLineBorder(Color.lightGray));
		tab.addTab("panelStudent", panelStudent);
		
		add(tab);
		bntAccept.addActionListener(this);
		bntCancel.addActionListener(this);
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		if(e.getSource() == bntAccept)
		{
			if(choosedFalcuty.equals("Chọn") == true)
				JOptionPane.showMessageDialog(this, "Hãy chọn khoa");
			else if(choosedMajor.equals("Chọn") == true)
				JOptionPane.showMessageDialog(this, "Hãy chọn ngành");
			else if(choosedClass.equals("Chọn") == true)
				JOptionPane.showMessageDialog(this, "Hãy chọn lớp");
			else
				JOptionPane.showMessageDialog(this, choosedClass + choosedFalcuty + choosedMajor);
		}
		else if(e.getSource() == bntCancel)
		{
			
		}
	}
	
	private void initialFacultyTab() throws Exception
	{
		lblFaculty = new JLabel("FACULTY MANAGEMENT");
		lblFaculty.setBackground(Color.lightGray);
		lblFaculty.setOpaque(true);
		JPanel panelTemp = new JPanel();
		JPanel panelLabel = new JPanel();
		JScrollPane scrPanelTempJScrollPane;
		lblFaculty.setPreferredSize(new Dimension(width - 30, 30));
		panelLabel.setPreferredSize(new Dimension(width , 70));
		panelLabel.add(lblFaculty);
		panelTemp.setPreferredSize(new Dimension(width - 30, height - 100));
		//panelTemp.add(panelLabel);
		
		int _height = 300;
		int _width = 300;
		Color colorTitle = Color.lightGray;
		String title = "Khoa Công Nghệ Thông Tin Khoa Công Nghệ Thông Tin";
		String iD = "CNTT1412";
		String name = "FIT-HUI";
		String numStudent = "200";
		String numTeacher = "20";
		tableComponent com1 = new tableComponent(_height, _width, colorTitle, title, iD, name, numStudent, numTeacher);
		tableComponent com2 = new tableComponent(_height, _width, colorTitle, title, iD, name, numStudent, numTeacher);
		tableComponent com3 = new tableComponent(_height, _width, colorTitle, title, iD, name, numStudent, numTeacher);
		tableComponent com4 = new tableComponent(_height, _width, colorTitle, title, iD, name, numStudent, numTeacher);
		tableComponent com5 = new tableComponent(_height, _width, colorTitle, title, iD, name, numStudent, numTeacher);
		tableComponent com6 = new tableComponent(_height, _width, colorTitle, title, iD, name, numStudent, numTeacher);
		tableComponent com7 = new tableComponent(_height, _width, colorTitle, title, iD, name, numStudent, numTeacher);
		tableComponent com8 = new tableComponent(_height, _width, colorTitle, title, iD, name, numStudent, numTeacher);

		
		panelTemp.setLayout(new GridLayout(2, 4));
		panelElement[0] = new JPanel();
		panelElement[0] = com1.getComponentPanel();
		panelElement[0].addMouseListener(this);
		panelElement[0].setPreferredSize(new Dimension(150, 150));
				
		panelElement[1] = new JPanel();
		panelElement[1] = com2.getComponentPanel();
		panelElement[1].addMouseListener(this);
		panelElement[1].setPreferredSize(new Dimension(150, 150));
		
		panelElement[2] = new JPanel();
		panelElement[2] = com3.getComponentPanel();
		panelElement[2].addMouseListener(this);
		panelElement[2].setPreferredSize(new Dimension(150, 150));
		
		panelElement[3] = new JPanel();
		panelElement[3] = com4.getComponentPanel();
		panelElement[3].addMouseListener(this);
		panelElement[3].setPreferredSize(new Dimension(150, 150));
		
		panelElement[4] = new JPanel();
		panelElement[4] = com5.getComponentPanel();
		panelElement[4].addMouseListener(this);	
		panelElement[4].setPreferredSize(new Dimension(150, 150));
		
		panelElement[5] = new JPanel();
		panelElement[5] = com6.getComponentPanel();
		panelElement[5].addMouseListener(this);
		panelElement[5].setPreferredSize(new Dimension(150, 150));
		
		panelElement[6] = new JPanel();
		panelElement[6] = com7.getComponentPanel();
		panelElement[6].addMouseListener(this);
		panelElement[6].setPreferredSize(new Dimension(150, 150));
		
		panelElement[7] = new JPanel();
		panelElement[7] = com8.getComponentPanel();
		panelElement[7].addMouseListener(this);
		panelElement[7].setPreferredSize(new Dimension(150, 150));
		
		panelTemp.add(panelElement[0]);
		panelTemp.add(panelElement[1]);
		panelTemp.add(panelElement[2]);
		panelTemp.add(panelElement[3]);
		panelTemp.add(panelElement[4]);
		panelTemp.add(panelElement[5]);
		panelTemp.add(panelElement[6]);
		panelTemp.add(panelElement[7]);
		
		
		panelFaculty.setSize(new Dimension(width, height - 40));
		
		scrPanelTempJScrollPane = new JScrollPane(panelTemp);
		boxFaculty.add(panelLabel);
		boxFaculty.add(scrPanelTempJScrollPane);

	}

	private void initialStudentTab() throws Exception
	{
		Font font = new Font("Arial", Font.PLAIN, 15);
		boxMain = Box.createVerticalBox();
		boxButton = Box.createHorizontalBox();
		boxStudentBirthDay = Box.createHorizontalBox();;
		boxStudentClass = Box.createHorizontalBox();
		boxStudentFacculty = Box.createHorizontalBox();
		boxStudentFirstName = Box.createHorizontalBox();
		boxStudentHometown = Box.createHorizontalBox();
		boxStudentLastName = Box.createHorizontalBox();
		boxStudentMajor = Box.createHorizontalBox();
		boxStudentSex = Box.createHorizontalBox();
		boxStudentInfo = Box.createHorizontalBox();
		boxStudentImage = Box.createHorizontalBox();
		
		lblStudentInfo = new  JLabel("THÔNG TIN SINH VIÊN");
		lblStudentInfo.setFont(new Font("Arial", Font.BOLD, 30));
		lblStudentInfo.setForeground(Color.red);
		lblStudentInfo.setOpaque(false);
		
		lblStudentClass = new JLabel("Lớp học");
		lblStudentClass.setFont(font);
		lblStudentBirthDay = new JLabel("Ngày sinh");
		lblStudentBirthDay.setFont(font);
		lblStudentFacculty = new JLabel("Khoa");
		lblStudentFacculty.setFont(font);
		lblStudentFirstName = new JLabel("Họ");
		lblStudentFirstName.setFont(font);
		lblStudentHometown = new JLabel("Quê quán");
		lblStudentHometown.setFont(font);
		lblStudentLastName = new JLabel("Tên");
		lblStudentLastName.setFont(font);
		lblStudentMajor = new JLabel("Chuyên ngành");
		lblStudentMajor.setFont(font);
		lblStudentSex = new JLabel("Giới tính");
		lblStudentSex.setFont(font);
		lblStudentImage = new JLabel("Ảnh sinh viên=?");
		lblStudentImage.setFont(new Font("Arial", Font.BOLD, 15));
		lblStudentImage.setForeground(Color.red);
		lblStudentImage.setOpaque(false);
		fcStudentImage = new JFileChooser();
		
		tfStudentBirthDay = new JTextField();
		tfStudentFirstName = new JTextField();
		tfStudentHometown = new JTextField();
		tfStudentLastName = new JTextField();
		
		bntAccept = new JButton("Lưu");
		bntCancel = new JButton("Hủy");
		
		rdFemale = new JRadioButton("Nữ", false);
		rdMale = new JRadioButton("Nam", true);
		grSex = new ButtonGroup();
		grSex.add(rdMale);
		grSex.add(rdFemale);
		boxStudentInfo.add(lblStudentInfo);
		boxStudentFirstName.add(lblStudentFirstName);
		boxStudentFirstName.add(Box.createHorizontalStrut(76));
		boxStudentFirstName.add(tfStudentFirstName);
		boxStudentLastName.add(lblStudentLastName);
		boxStudentLastName.add(Box.createHorizontalStrut(70));
		boxStudentLastName.add(tfStudentLastName);
		boxStudentSex.add(lblStudentSex);
		boxStudentSex.add(Box.createHorizontalStrut(40));
		boxStudentSex.add(rdFemale);
		boxStudentSex.add(rdMale);
		boxStudentSex.add(Box.createHorizontalStrut(170));		
		boxStudentBirthDay.add(lblStudentBirthDay);
		boxStudentBirthDay.add(Box.createHorizontalStrut(30));
		boxStudentBirthDay.add(tfStudentBirthDay);
		boxStudentHometown.add(lblStudentHometown);
		boxStudentHometown.add(Box.createHorizontalStrut(30));
		boxStudentHometown.add(tfStudentHometown);
		boxStudentFacculty.add(lblStudentFacculty);
		boxStudentFacculty.add(Box.createHorizontalStrut(60));
		boxStudentMajor.add(lblStudentMajor);
		boxStudentClass.add(lblStudentClass);
		boxStudentClass.add(Box.createHorizontalStrut(39));
		boxStudentImage.add(lblStudentImage);
		boxStudentImage.add(fcStudentImage);
		initialCombobox();
		boxButton.add(bntAccept);
		boxButton.add(bntCancel);
		
		boxMain.add(Box.createVerticalStrut(20));
		boxMain.add(boxStudentInfo);
		boxMain.add(Box.createVerticalStrut(30));
		boxMain.add(boxStudentFirstName);
		boxMain.add(Box.createVerticalStrut(20));
		boxMain.add(boxStudentLastName);
		boxMain.add(Box.createVerticalStrut(20));
		boxMain.add(boxStudentSex);
		boxMain.add(Box.createVerticalStrut(20));
		boxMain.add(boxStudentBirthDay);
		boxMain.add(Box.createVerticalStrut(20));
		boxMain.add(boxStudentHometown);
		boxMain.add(Box.createVerticalStrut(20));
		boxMain.add(boxStudentFacculty);
		boxMain.add(Box.createVerticalStrut(20));
		boxMain.add(boxStudentMajor);
		boxMain.add(Box.createVerticalStrut(20));
		boxMain.add(boxStudentClass);
		boxMain.add(Box.createVerticalStrut(20));
		boxMain.add(boxButton);
		//boxMain.add(Box.createVerticalStrut(20));
		
		panelstudentInfo = new JPanel();
		panelStudentImage = new JPanel();
		Box box1, box2, box3;
		box1 = Box.createVerticalBox();
		box2 = Box.createHorizontalBox();
		box3 = Box.createHorizontalBox();
		panelstudentInfo.add(boxMain);
		box2.add(fcStudentImage);
		box3.add(lblStudentImage);
		//box3.add(Box.createHorizontalStrut(100));
		box1.add(box2);
		box1.add(box3);
		panelStudent.add(panelstudentInfo);
		panelStudent.add(box1);
		//add(panelStudent);
		
	}
	private void initialCombobox()
	{
		//Get các giá trị về faculty, major, class tại đây
		String[] _faculty = {"Chọn", "BCA","MCA","PPC","CIC"};;
		String[] _major = {"Chọn", "BCA","MCA","PPC","CIC"};;
		String[] _class = {"Chọn", "BCA","MCA","PPC","CIC"};;
		cbbStudentFaculty = new JComboBox<>(_faculty);
		cbbStudentClass = new JComboBox<>(_class);
		cbbStudentMajor = new JComboBox<>(_major);
		boxStudentClass.add(cbbStudentClass);
		boxStudentFacculty.add(cbbStudentFaculty);
		boxStudentMajor.add(cbbStudentMajor);
		cbbStudentFaculty.addItemListener(new ItemListener(){
			  public void itemStateChanged(ItemEvent ie){
			  choosedFalcuty = (String)cbbStudentFaculty.getSelectedItem();
			  }
			  });
		cbbStudentClass.addItemListener(new ItemListener(){
			  public void itemStateChanged(ItemEvent ie){
			  choosedClass = (String)cbbStudentClass.getSelectedItem();
			  }
			  });
		cbbStudentMajor.addItemListener(new ItemListener(){
			  public void itemStateChanged(ItemEvent ie){
			  choosedMajor = (String)cbbStudentMajor.getSelectedItem();
			  }
			  });
	}
	public int getScreenWorkingWidth() 
	{
	    return java.awt.GraphicsEnvironment.getLocalGraphicsEnvironment().getMaximumWindowBounds().width;
	}

	public int getScreenWorkingHeight(){
	    return java.awt.GraphicsEnvironment.getLocalGraphicsEnvironment().getMaximumWindowBounds().height - 5;
	}

	@Override
	public void mouseClicked(MouseEvent e) {
		// TODO Auto-generated method stub
		if(e.getSource() == panelElement[0])
			JOptionPane.showMessageDialog(this, "panel 0");
		if(e.getSource() == panelElement[1])
			JOptionPane.showMessageDialog(this, "panel 1");
		if(e.getSource() == panelElement[2])
			JOptionPane.showMessageDialog(this, "panel 2");
		if(e.getSource() == panelElement[3])
			JOptionPane.showMessageDialog(this, "panel 3");
		if(e.getSource() == panelElement[4])
			JOptionPane.showMessageDialog(this, "panel 4");
		if(e.getSource() == panelElement[5])
			JOptionPane.showMessageDialog(this, "panel 5");
		if(e.getSource() == panelElement[6])
			JOptionPane.showMessageDialog(this, "panel 6");
		if(e.getSource() == panelElement[7])
			JOptionPane.showMessageDialog(this, "panel 7");
	}

	@Override
	public void mouseEntered(MouseEvent e) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void mouseExited(MouseEvent e) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void mousePressed(MouseEvent e) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void mouseReleased(MouseEvent e) {
		// TODO Auto-generated method stub
		
	}

}
